class Task8:
    def run(self, node):
        if node is None:
            return None

        if node.right is not None:
            left_most = node.right
            while left_most.left is not None:
                left_most = left_most.left
            return left_most

        elif node.parent is not None:
            temp = node
            while temp.parent is not None and temp.parent.right == temp:
                temp = temp.parent
            if temp.parent is None:
                return None
            else:
                return temp.parent

        else:
            return None
